Skip to content

Commit 49218e8

Browse files
authored
Merge branch 'master' into multi-channels
2 parents 2ac125c + 585e6cc commit 49218e8

13 files changed

Lines changed: 462 additions & 158 deletions

File tree

config.example.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
2+
"host": "*",
3+
"port: 6542,
24
"token": "",
35
"guildId": 0000000001,
46
"ownerId": 0000000001,

src/Bot.cs

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace iPhoneController
22
{
33
using System;
4-
using System.Collections.Generic;
54
using System.Linq;
65
using System.Threading.Tasks;
76

@@ -14,6 +13,7 @@
1413
using iPhoneController.Configuration;
1514
using iPhoneController.Diagnostics;
1615
using iPhoneController.Extensions;
16+
using iPhoneController.Net;
1717

1818
public class Bot
1919
{
@@ -22,6 +22,7 @@ public class Bot
2222
private readonly DiscordClient _client;
2323
private readonly CommandsNextModule _commands;
2424
private readonly Config _config;
25+
private readonly HttpServer _server;
2526

2627
public Bot(Config config)
2728
{
@@ -85,6 +86,7 @@ public Bot(Config config)
8586
_commands.CommandExecuted += Commands_CommandExecuted;
8687
_commands.CommandErrored += Commands_CommandErrored;
8788
_commands.RegisterCommands<PhoneControl>();
89+
_server = new HttpServer(_config.Host, _config.Port);
8890
}
8991

9092
public void Start()
@@ -93,32 +95,17 @@ public void Start()
9395
_logger.Info("Connecting to Discord...");
9496

9597
_client.ConnectAsync();
98+
_server.Start();
99+
96100
}
97101

98-
public static Dictionary<string, string> GetDevices()
102+
public void Stop()
99103
{
100-
var devices = new Dictionary<string, string>();
101-
var output = Utils.Shell.Execute("ios-deploy", "-c device_identification", out var exitCode);
102-
if (string.IsNullOrEmpty(output) || exitCode != 0)
103-
{
104-
// Failed
105-
return devices;
106-
}
107-
108-
var split = output.Split('\n');
109-
foreach (var line in split)
110-
{
111-
if (!line.ToLower().Contains("found"))
112-
continue;
104+
_logger.Trace("Stop");
105+
_logger.Info("Disconnecting Discord client...");
113106

114-
var name = line.GetBetween("Found ", " (");
115-
var uuid = line.GetBetween("'", "'");
116-
if (!devices.ContainsKey(name))
117-
{
118-
devices.Add(name, uuid);
119-
}
120-
}
121-
return devices;
107+
_client.DisconnectAsync();
108+
_server.Stop();
122109
}
123110

124111
#region Discord Events
@@ -149,7 +136,7 @@ private async Task Client_ClientErrored(ClientErrorEventArgs e)
149136
private async Task Commands_CommandExecuted(CommandExecutionEventArgs e)
150137
{
151138
// let's log the name of the command and user
152-
e.Context.Client.DebugLogger.LogMessage(LogLevel.Info, Strings.BotName, $"{e.Context.User.Username} successfully executed '{e.Command.QualifiedName}'", DateTime.Now);
139+
e.Context.Client.DebugLogger.LogMessage(DSharpPlus.LogLevel.Info, Strings.BotName, $"{e.Context.User.Username} successfully executed '{e.Command.QualifiedName}'", DateTime.Now);
153140

154141
// since this method is not async, let's return
155142
// a completed task, so that no additional work
@@ -159,7 +146,7 @@ private async Task Commands_CommandExecuted(CommandExecutionEventArgs e)
159146

160147
private async Task Commands_CommandErrored(CommandErrorEventArgs e)
161148
{
162-
e.Context.Client.DebugLogger.LogMessage(LogLevel.Error, Strings.BotName, $"{e.Context.User.Username} tried executing '{e.Command?.QualifiedName ?? e.Context.Message.Content}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no message>"}", DateTime.Now);
149+
e.Context.Client.DebugLogger.LogMessage(DSharpPlus.LogLevel.Error, Strings.BotName, $"{e.Context.User.Username} tried executing '{e.Command?.QualifiedName ?? e.Context.Message.Content}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no message>"}", DateTime.Now);
163150

164151
// let's check if the error is a result of lack of required permissions
165152
if (e.Exception is DSharpPlus.CommandsNext.Exceptions.ChecksFailedException)
@@ -214,11 +201,11 @@ private void DebugLogger_LogMessageReceived(object sender, DebugLogMessageEventA
214201
ConsoleColor color;
215202
switch (e.Level)
216203
{
217-
case LogLevel.Error: color = ConsoleColor.DarkRed; break;
218-
case LogLevel.Warning: color = ConsoleColor.Yellow; break;
219-
case LogLevel.Info: color = ConsoleColor.White; break;
220-
case LogLevel.Critical: color = ConsoleColor.Red; break;
221-
case LogLevel.Debug: default: color = ConsoleColor.DarkGray; break;
204+
case DSharpPlus.LogLevel.Error: color = ConsoleColor.DarkRed; break;
205+
case DSharpPlus.LogLevel.Warning: color = ConsoleColor.Yellow; break;
206+
case DSharpPlus.LogLevel.Info: color = ConsoleColor.White; break;
207+
case DSharpPlus.LogLevel.Critical: color = ConsoleColor.Red; break;
208+
case DSharpPlus.LogLevel.Debug: default: color = ConsoleColor.DarkGray; break;
222209
}
223210

224211
//Source

src/Commands/PhoneControl.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using DSharpPlus.Entities;
1212

1313
using iPhoneController.Diagnostics;
14-
using iPhoneController.Extensions;
1514
using iPhoneController.Utils;
1615

1716
//TODO: Restart all devices
@@ -52,7 +51,7 @@ public async Task ListDevicesAsync(CommandContext ctx,
5251
if (!string.IsNullOrEmpty(machineName) && string.Compare(machineName, Environment.MachineName, true) != 0)
5352
return;
5453

55-
var devices = GetDevices();
54+
var devices = Devices.GetAll();
5655
var keys = devices.Keys.ToList();
5756
/*
5857
var pages = new List<string>();
@@ -92,7 +91,7 @@ public async Task ListDevicesAsync(CommandContext ctx,
9291

9392
private List<string> SplitPages()
9493
{
95-
var devices = GetDevices();
94+
var devices = Devices.GetAll();
9695
var keys = devices.Keys.ToList();
9796
var pages = new List<string>();
9897
var maxDevicePerPage = 20;
@@ -134,7 +133,7 @@ public async Task ScreenshotAsync(CommandContext ctx,
134133
return;
135134

136135
//TODO: Check if idevicescreenshot is installed.
137-
var devices = GetDevices();
136+
var devices = Devices.GetAll();
138137
var rebootDevices = phoneNames.Replace(", ", ",").Split(',');
139138
var devicesFailed = new Dictionary<string, string>();
140139
for (var i = 0; i < rebootDevices.Length; i++)
@@ -197,7 +196,7 @@ public async Task IosVersionAsync(CommandContext ctx,
197196
return;
198197

199198
var dict = new Dictionary<string, string>();
200-
var devices = GetDevices();
199+
var devices = Devices.GetAll();
201200
var keys = devices.Keys.ToList();
202201
keys.Sort();
203202

@@ -263,7 +262,7 @@ public async Task RebootAsync(CommandContext ctx,
263262

264263
//TODO: Check if idevicediagnostics is installed.
265264

266-
var devices = GetDevices();
265+
var devices = Devices.GetAll();
267266
var rebootDevices = phoneNames.Replace(", ", ",").Split(',');
268267
for (var i = 0; i < rebootDevices.Length; i++)
269268
{
@@ -299,7 +298,7 @@ public async Task ShutdownAsync(CommandContext ctx,
299298
return;
300299

301300
//TODO: Check if idevicediagnostics is installed.
302-
var devices = GetDevices();
301+
var devices = Devices.GetAll();
303302
var shutdownDevices = phoneNames.Replace(", ", ",").Split(',');
304303
for (var i = 0; i < shutdownDevices.Length; i++)
305304
{
@@ -366,7 +365,7 @@ public async Task RemovePoGoAsync(CommandContext ctx,
366365
if (!IsValidChannel(ctx.Channel.Id))
367366
return;
368367

369-
var devices = GetDevices();
368+
var devices = Devices.GetAll();
370369
var removeAppDevices = phoneNames.Replace(", ", ",").Split(',');
371370
for (var i = 0; i < removeAppDevices.Length; i++)
372371
{
@@ -414,32 +413,6 @@ private bool IsValidChannel(ulong channelId)
414413
return _dep.Config.ChannelIds.Count == 0 || _dep.Config.ChannelIds.Contains(channelId);
415414
}
416415

417-
private Dictionary<string, string> GetDevices()
418-
{
419-
var devices = new Dictionary<string, string>();
420-
var output = Shell.Execute("ios-deploy", "-c device_identification", out var exitCode);
421-
if (string.IsNullOrEmpty(output))// || exitCode != 0)
422-
{
423-
// Failed
424-
return devices;
425-
}
426-
427-
var split = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
428-
foreach (var line in split)
429-
{
430-
if (!line.ToLower().Contains("found"))
431-
continue;
432-
433-
var uuid = line.GetBetween("Found ", " (");
434-
var name = line.GetBetween("'", "'");
435-
if (!devices.ContainsKey(name))
436-
{
437-
devices.Add(name, uuid);
438-
}
439-
}
440-
return devices;
441-
}
442-
443416
#endregion
444417
}
445418
}

src/Configuration/Config.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ public class Config
1414

1515
#region Properties
1616

17+
[JsonProperty("host")]
18+
public string Host { get; set; }
19+
20+
[JsonProperty("port")]
21+
public ushort Port { get; set; }
22+
1723
[JsonProperty("guildId")]
1824
public ulong GuildId { get; set; }
1925

@@ -39,6 +45,8 @@ public class Config
3945
public Config()
4046
{
4147
ChannelIds = new List<ulong>();
48+
Host = "*";
49+
Port = 6542;
4250
RequiredRoles = new List<ulong>();
4351
}
4452

0 commit comments

Comments
 (0)